<?php

/**
 * This is the model class for table "cart".
 *
 * The followings are the available columns in table 'cart':
 * @property integer $id
 * @property string $customerid
 * @property string $createtime
 */
class Cart extends CommonActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Cart the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cart';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('status', 'numerical', 'integerOnly'=>true),
			array('customerid', 'length', 'max'=>45),
			array('createtime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, customerid, createtime, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'customerid' => 'Customerid',
			'createtime' => 'Createtime',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('customerid',$this->customerid,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function addProduct($customerId,$product){
		$productId = $product['id'];
		try {
			$cart = Cart::model()->findByAttributes(array('customerid'=>$customerId));
			if(empty($cart)){
				$cart = new Cart ();
				$cart->customerid = $customerId;
				$cart->createtime = date ( "Y-m-d H:i:s" );
				$cart->save();
			}
			$cartMap = CartProductMap::model ()->findByAttributes ( array (
					'customerid' => $customerId,
					'cartid' => $cart->id,
					'productid' => $productId
			) );
			if (empty ( $cartMap )) {
				$cartMap = new CartProductMap ();
			}else{
				$cartMap->num = $cartMap->num+1;
				$cartMap->total = $cartMap->num * $cartMap->amount;
				if($cartMap->update(array('num','total'))){
					return true;
				}else{
					return false;
				}
			}
			$cartMap->customerid = $customerId;
			$cartMap->cartid = $cart->id;
			$cartMap->productid = $productId;
			$cartMap->num = 1;
			$cartMap->amount = $product['price'];
			$cartMap->currency = $product['currency'];
			$cartMap->total = $product['price'];
			if($cartMap->save()){
				return true;
			}else{
				return false;
			}
		} catch ( CException $e ) {
			return false;
		}
	}
}